package com.doule.farmerassistant;

import java.util.ArrayList;

import com.doule.farmerassistant.task.TaskDetail;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;

/**
 * ContactBaseAdapter class.
 * 
 * @author Doule
 * 
 */
public class TasksCheckAdapter extends BaseAdapter {
    /**
     * Contact data list.
     */
    private final ArrayList<TaskDetail> mData;
    /**
     * 
     */
    private LayoutInflater inflater = null;

    /**
     * Group id.
     */
    // private String mGroupId = null;
    // /**
    // * admin id.
    // */
    // private String mAdmin = null;

    // /**
    // * member list.
    // */
    // private List<String> mMembers = null;

    /**
     * ContactBaseAdapter construct.
     * 
     * @param activity
     *            MessengerActivity
     * @param data
     *            Data of Contact
     */
    public TasksCheckAdapter(final Context activity,
            final ArrayList<TaskDetail> data) {
        mData = data;
        inflater = LayoutInflater.from(activity);
        clearCheckMember();
    }

    /**
     * get Contact list.
     * 
     * @return mData
     */
    public final ArrayList<TaskDetail> getContactList() {
        return mData;
    }

    // /**
    // *
    // */
    // private void sort() {
    // if (mData != null && mData.size() > 0) {
    // Collections.sort(mData, new SortByMember());
    // }
    // }

    /**
     * clean all check box.
     */
    private void clearCheckMember() {
        for (int i = 0; i < mData.size(); i++) {
            mData.get(i).setChecked(false);
        }
    }

    // /**
    // * If ContactDataWrapper in mMembers select it.
    // */
    // private void checkMember() {
    // if (mMembers == null || mMembers.size() < 0) {
    // return;
    // }
    //
    // for (int i = 0; i < mData.size(); i++) {
    // CatContentHolder data = mData.get(i);
    // String id = data.getDeviceId();
    // if (mMembers.contains(id)) {
    // mData.remove(i);
    // data.setChecked(true);
    // mData.add(0, data);
    // }
    // }
    // }

    @Override
    public final int getCount() {
        return mData.size();
    }

    @Override
    public final Object getItem(final int position) {
        return mData.get(position);
    }

    @Override
    public final long getItemId(final int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public final View getView(final int position, final View convertView,
            final ViewGroup parent) {
        final ViewHolder viewHolder;
        View vi = convertView;
        final TaskDetail data = mData.get(position);
        // final String id = data.getDeviceId();
        if (convertView == null) {
            viewHolder = new ViewHolder();
            vi = inflater.inflate(R.layout.task_row, null);
            viewHolder.name = (TextView) vi.findViewById(R.id.tv_task_name);
            viewHolder.name.setVisibility(View.GONE);
            viewHolder.checkBox = (CheckBox) vi.findViewById(R.id.cb_task_name);
            viewHolder.checkBox.setVisibility(View.VISIBLE);
            vi.setTag(viewHolder);
            checkBoxListenerInit(viewHolder);
        } else {
            viewHolder = (ViewHolder) vi.getTag();
        }
        viewHolder.data = data;
        String st = data.getName();
        viewHolder.checkBox.setText(st);

        viewHolder.checkBox.setChecked(data.isChecked());

        return vi;
    }

    /**
     * 
     * @param viewHolder
     *            view holder.
     */
    private void checkBoxListenerInit(final ViewHolder viewHolder) {
        viewHolder.checkBox
                .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(final CompoundButton arg0,
                            final boolean checked) {
                        // String myId =
                        // MessengerActivity.getDeviceId(mActivity);
                        // if (mAdmin != null && !mAdmin.equals(myId)) {
                        // String memberId = viewHolder.data.getDeviceId();
                        // if (mMembers.size() > 0
                        // && mMembers.contains(memberId)) {
                        // viewHolder.checkBox.setChecked(true);
                        // Toast.makeText(
                        // mActivity,
                        // "you are not admin, can't delete member!",
                        // Toast.LENGTH_SHORT).show();
                        // return;
                        // }
                        // }
                        viewHolder.data.setChecked(checked);
                    }

                });
    }

    /**
     * @author Doule
     */
    private class ViewHolder {
        /**
         * dispaly contact name.
         */
        private TextView name;

        /**
         * checkbox for select or unselect.
         */
        private CheckBox checkBox;

        /**
         * data binding to this view holder.
         */
        private TaskDetail data;
    }

}
